package com.fit.utils.ee;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * request工具
 * @author hcl
 *
 */
public class RequestUtil {
	
	/**
	 * 判断是否是ajax请求
	 * @param request
	 * @return
	 */
	public static boolean isAjax(HttpServletRequest request){
		String str = request.getHeader("X-Requested-With");
		return (str == null || "".equals(str.trim())) ? false : true;
	}
	
	/**
	 * @describe: 解析UrlParam 参数
	 * @author: hcl  
	 * @date: 2018/7/6 11:26
	 * @param: [strUrlParam]  
	 * @return java.util.Map<java.lang.String,java.lang.String>  
	 */
	public static Map<String,String> analysisUrlParam(String strUrlParam){
		Map<String, String> mapRequest = new HashMap<String, String>();
		if (strUrlParam == null) {
			return mapRequest;
		}
		String[] arrSplit = null;
		//每个键值为一组
		arrSplit = strUrlParam.split("[&]");
		for (String strSplit : arrSplit) {
			String[] arrSplitEqual = null;
			arrSplitEqual = strSplit.split("[=]");
			//解析出键值
			if (arrSplitEqual.length > 1) {
				//正确解析
				mapRequest.put(arrSplitEqual[0], arrSplitEqual[1]);

			} else {
				if (arrSplitEqual[0] != "") {
					//只有参数没有值，不加入
					mapRequest.put(arrSplitEqual[0], "");
				}
			}
		}
		return mapRequest;
	}
}
